package com.bea.ftd.dashboard.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bea.ftd.common.ValidationException;
import com.bea.ftd.common.entity.MsgInConf;
import com.bea.ftd.dashboard.domain.AjaxResponse;
import com.bea.ftd.dashboard.enums.ResponseStatus;
import com.bea.ftd.dashboard.service.IMsgInConfService;

@RestController
@RequestMapping("/msgInConf")
public class MsgInConfController {
	private Logger LOGGER = LoggerFactory.getLogger(MsgInConfController.class);

	@Autowired
	private IMsgInConfService msgInConfServiceImpl;

	@RequestMapping("/listAll")
	public List<MsgInConf> listAllMsgInConfs() {
		return msgInConfServiceImpl.getAllMsgInConfs();
	}

	@RequestMapping("/saveOrUpdate")
	public AjaxResponse addMsgInConf(MsgInConf msgInConf) {
		LOGGER.info("Save or update msgInConf - " + msgInConf.toString());
		try {
			msgInConfServiceImpl.addMsgInConf(msgInConf);
			return new AjaxResponse(ResponseStatus.SUCCESS);
		} catch (ValidationException e) {
			LOGGER.error("输入参数错误：" + e.getErrorMessage());
			return new AjaxResponse(ResponseStatus.FAIL, e.getErrorMessage());
		} catch (Exception e) {
			LOGGER.error("Error in saving msgInConf to DB. " + e.getMessage());
			return new AjaxResponse(ResponseStatus.FAIL, e.getMessage());
		}

	}

	@RequestMapping("/addMsgInConfFromExample")
	public AjaxResponse addMsgInConfFromExample(String exampleData, String apiId) {

		try {
			msgInConfServiceImpl.addMsgInConfFromExample(exampleData, apiId);
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResponse(ResponseStatus.FAIL, "报文解析错误");
		}
		return new AjaxResponse(ResponseStatus.SUCCESS);
	}

	@RequestMapping("/deleteMsgInConfContent")
	public AjaxResponse deleteMsgInConfContent(String apiId) {
		try {
			msgInConfServiceImpl.deleteMsgInConfContent(apiId);
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResponse(ResponseStatus.FAIL, "报文解析错误");
		}
		return new AjaxResponse(ResponseStatus.SUCCESS);
	}
}
